package com.ruoyi.basedata.service.impl;

import java.util.List;

import com.ruoyi.basedata.domain.Phone;
import com.ruoyi.basedata.domain.vo.SuppliesVO;
import com.ruoyi.basedata.mapper.PhoneMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.SuppliesMapper;
import com.ruoyi.basedata.domain.Supplies;
import com.ruoyi.basedata.service.ISuppliesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商Service业务层处理
 *
 * @author lvql
 * @date 2024-10-25
 */
@Slf4j
@Service
public class SuppliesServiceImpl implements ISuppliesService {
    @Autowired
    private SuppliesMapper suppliesMapper;

    @Autowired
    private PhoneMapper phoneMapper;

    /**
     * 查询供应商
     *
     * @param id 供应商主键
     * @return 供应商
     */
    @Override
    public SuppliesVO selectSuppliesById(Integer id) {
        return suppliesMapper.selectSuppliesById(id);
    }

    @Override
    public int getSupCount() {
        return suppliesMapper.getSupCount();
    }

    /**
     * 查询供应商列表
     *
     * @param supplies 供应商
     * @return 供应商
     */
    @Override
    public List<SuppliesVO> selectSuppliesList(Supplies supplies) {
        return suppliesMapper.selectSuppliesList(supplies);
    }

    /**
     * 新增供应商
     *
     * @param supplies 供应商
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSupplies(SuppliesVO supplies) {
        Phone phone = new Phone();
        phone.setPhone(supplies.getPhone());
        phone.setCreateBy(supplies.getCreateBy());
        phone.setPerCode(supplies.getSupCode());

        phoneMapper.insertPhone(phone);
        return suppliesMapper.insertSupplies(supplies);
    }

    /**
     * 修改供应商
     *
     * @param supplies 供应商
     * @return 结果
     */
    @Override
    public int updateSupplies(SuppliesVO supplies) {
        Phone phone = new Phone();
        phone.setPerCode(supplies.getSupCode());
        phone.setModifyBy(supplies.getModifyBy());
        phone.setPhone(supplies.getPhone());
        phoneMapper.updatePhone(phone);
        return suppliesMapper.updateSupplies(supplies);
    }

    /**
     * 批量删除供应商
     *
     * @param ids 需要删除的供应商主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSuppliesByIds(Integer[] ids) {
        for (Integer id : ids) {
            SuppliesVO suppliesVO = suppliesMapper.selectSuppliesById(id);
            int i = phoneMapper.deletePhoneByPerCode(suppliesVO.getSupCode());
            log.info("供应商删除,电话删除结果{}",(i > 0 ? "是" : "否"));
        }
        return suppliesMapper.deleteSuppliesByIds(ids);
    }

    /**
     * 删除供应商信息
     *
     * @param id 供应商主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSuppliesById(Integer id) {

        return suppliesMapper.deleteSuppliesById(id);
    }
}
